package top.jnaw.jee.platform.model;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import java.util.List;
import top.jnaw.jee.platform.Consts;
import top.jnaw.jee.platform.Consts.Result;
import top.jnaw.jee.platform.model.base.BaseBadge;
import top.jnaw.jee.utils.Html2Test;
import top.jnaw.jee.utils.Log;
import top.jnaw.jee.utils.ModelResult;
import top.jnaw.jee.utils.Models;

/**
 * Generated by JFinal.
 */
@SuppressWarnings("serial")
public class Badge extends BaseBadge<Badge> {

  public static final Badge dao = new Badge().dao();

  public static int add(int fid, String title, String content, String type) {
    ModelResult result = new ModelResult(Consts.Result.kModelSaveError);
    Badge badge = new Badge();
    badge.setFid(fid);
    badge.setType(type);
    badge.setTitle(title);
    badge.setContent(content);
    boolean r = badge.save();
    int id = -1;
    if (r) {
      id = badge.getId();
    }
    return id;
  }

  public static int update(int id, int fid, String title,
      String content, String type) {
    ModelResult result = new ModelResult(Consts.Result.kModelSaveError);
    Badge badge = Badge.dao.findById(id);
    int rid = -1;
    if (null != badge) {
      badge.setFid(fid);
      badge.setType(type);
      badge.setTitle(title);
      badge.setContent(content);
      boolean r = badge.update();
//      if (r) {
//        result.setObject(badge);
//        result.setResult(r ? Result.kSuccess : Result.kNext);
//      }
      if (r) {
        rid = badge.getId();
      }
    }
    return rid;
  }

  public static boolean delete(int id) {
    boolean r = false;
    r = Badge.dao.deleteById(id);
    return r;
  }

  public static ModelResult query(int id) {
    ModelResult result = new ModelResult(Result.kNull);
    String key = "badge-query";
    List<Record> list = Db.find(Db.getSql(key), id);
    if (null != list && list.size() > 0) {
      JSONObject object = Models.compressList(list);
      result.setObject(object);
      result.setResult(Consts.Result.kSuccess);
    }
    return result;
  }

  public static String getType(int id) {
    Badge badge = Badge.dao.findById(id);
    String type = "";
    if (null != badge) {
      type = badge.getType();
    }
    return type;
  }

  public static JSONObject list(int page, int size, String keyWord) {
    String sec = Db.getSql("badge-list-sec");
    String exp = Db.getSql("badge-list-exp");
    if (StrKit.notBlank(keyWord)) {
      exp +=
          "\n" + "AND (b.title like concat('%','" + keyWord
              + "','%') or  b.content like concat('%','" + keyWord + "','%'))";
    }
    exp += " order by create_time desc ";
    Page<Record> pages = Db.paginate(page, size, sec, exp);
    return Models.compressPage(pages);
  }

  public static ModelResult listAll(String openid, String type) {
    ModelResult result = new ModelResult(Result.kNull);
    String key = "badge-listAll";
    String sql = Db.getSql(key);
    if (StrKit.notBlank(type)) {
      sql += " and b.type='" + type + "'";
    }
    sql += " order by bu.openid desc ";
    List<Record> list = Db.find(sql, openid);
    if (null != list && list.size() > 0) {
      result.setObject(list);
    }
    result.setResult(Consts.Result.kSuccess);
    return result;
  }

}
